package com.fjh.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.fjh.common.CodeMsg;
import com.fjh.common.exces.BusinessException;
import com.fjh.domain.BusCar;
import com.fjh.domain.BusCustomer;
import com.fjh.domain.BusRent;
import com.fjh.dto.CarDto;
import com.fjh.mapper.BusCarMapper;
import com.fjh.mapper.BusCustomerMapper;
import com.fjh.mapper.BusRentMapper;
import com.fjh.realm.ActiveUser;
import com.fjh.service.CarService;
import com.fjh.vo.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class CarServiceImpl implements CarService {
    @Autowired
    private BusCarMapper carMapper;

    @Autowired
    private BusRentMapper rentMapper;

    @Autowired
    private BusCustomerMapper busCustomerMapper;

    /**
     * 分页查询
     *
     * @param carDto
     * @return
     */
    @Override
    public PageInfo<BusCar> findByPage(CarDto carDto) {
        Page<Object> page = PageHelper.startPage(carDto.getPage(), carDto.getLimit());

        List<BusCar> list = carMapper.selectCars(carDto);

        return new PageInfo<>(page.getTotal(), list);
    }

    /**
     * 新增数据
     *
     * @param carDto
     * @return
     */
    @Override
    public Boolean save(CarDto carDto) {
        // 1 校验数据
        validate(carDto);
        // 2 属性copy
        BusCar busCar = new BusCar();
        BeanUtils.copyProperties(carDto, busCar);
        // 3 填充默认值
        busCar.setCreateTime(new Date());
        busCar.setVersion(0);
        busCar.setIsRent(0);
        return carMapper.insert(busCar) > 0;
    }

    /**
     * 校验数据
     *
     * @param carDto
     */
    private void validate(CarDto carDto) {


        if (carDto.getNum() == null) {
            throw new IllegalArgumentException("车牌号不为空");
        }
        if (carDto.getPrice() == null) {
            throw new IllegalArgumentException("车的价格不能为空");
        }
        if (carDto.getRentPrice() == null) {
            throw new IllegalArgumentException("车的出租价格不能为空");
        }
    }

    /**
     * 修改数据
     *
     * @param carDto
     * @return
     */
    @Override
    public Boolean updateById(CarDto carDto) {
        if (carDto.getId() == null) {
            throw new IllegalArgumentException("id不能为空");
        }
        // 1 校验数据
        validate(carDto);
        // 2 属性copy
        BusCar busCar = new BusCar();
        BeanUtils.copyProperties(carDto, busCar);
        // 3 填充默认值
        busCar.setCreateTime(new Date());
        return carMapper.updateByPrimaryKeySelective(busCar) > 0;
    }

    /**
     * 删除数据
     *
     * @param ids
     * @return
     */
    @Override
    public Boolean batchDelete(Integer[] ids) {
        return carMapper.batchDelete(ids);
    }

    /**
     * 车辆的出租
     *
     * @param carId
     * @param idCard
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    @Transactional
    public Boolean rentCar(Integer carId, String idCard, String startTime, String endTime) {
        // 1 校验车辆
        BusCar car = validateCarForRent(carId);
        // 2 校验 客户
        BusCustomer customer = validateCustomerForRent(idCard);
        // 3 写租车表
        int insert = createCarRent(startTime, endTime, car, customer);
        //4 修改车辆的状态
        if (insert > 0) {
            // 修状态为已经借出
            car.setIsRent(1);
            carMapper.updateByPrimaryKey(car);
        }
        return insert > 0;
    }

    private int createCarRent(String startTime, String endTime, BusCar car, BusCustomer customer) {
        BusRent busRent = new BusRent();
        // 复制car的数据
        BeanUtils.copyProperties(car, busRent);
        // 复制customer的数据
        BeanUtils.copyProperties(customer, busRent);
        // 填充默认的数据
        busRent.setBeginTime(startTime);
        busRent.setEndTime(endTime);
        busRent.setFlag(1);

        // 填充默认的值
        busRent.setCreateTime(new Date());
        busRent.setUpdateTime(new Date());
//        TODO 此处有问题
        // 设置业务员的id
        busRent.setUserId(getCurrentUser());
        // 让租车记录的id 自己增长
        busRent.setId(null);
        return rentMapper.insert(busRent);
    }

    /**
     * 获取业务员的id
     *
     * @return
     */
    private Integer getCurrentUser() {
        ActiveUser principal = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        return principal.getUser().getId();
    }


    /**
     * 租车时校验客户的身份
     *
     * @param idCard
     * @return
     */
    private BusCustomer validateCustomerForRent(String idCard) {
        BusCustomer busCustomer = busCustomerMapper.selectByIdCard(idCard);
        if (busCustomer == null) {
            throw new IllegalArgumentException("客户的身份证输入错误");
        }
        // 校验客户是否被禁用
        // ....
        return busCustomer;
    }

    /**
     * 租车时校验车辆的数据
     *
     * @param carId
     * @return
     */
    private BusCar validateCarForRent(Integer carId) {
        BusCar car = carMapper.selectByPrimaryKey(carId);
        if (car == null) {
            throw new IllegalArgumentException("车辆的id输入错误");
        }
        if (car.getIsRent() != null && car.getIsRent() == 1) {
            throw new BusinessException(CodeMsg.CAR_USED.code ,CodeMsg.CAR_USED.msg) ;
        }
        // ... 车辆等等的校验全部写完
        return car;
    }
}
